package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;
import game2dai.maths.MathUtils;
import game2dai.steering.SBF;

public class KeeperReturnHome extends State implements Constants {

	private static KeeperReturnHome instance = null;
	
	public static KeeperReturnHome state(){
		if(instance == null){
			instance = new KeeperReturnHome();
			instance.name = "Return home";
		}
		return instance;
	}
	
	
	@Override
	public void enter(BaseEntity owner) {
		GoalKeeper keeper = (GoalKeeper) owner;
		keeper.targetPos.set(keeper.team.pitch.getRegion(keeper.regionToUse).centre);
		keeper.targetPos.add(MathUtils.randomClamped(), MathUtils.randomClamped());
		keeper.AP().arriveOn(keeper.targetPos, SBF.FAST);
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		GoalKeeper keeper = (GoalKeeper) owner;
		if(keeper.team.pitch.getRegion(keeper.regionToUse).contains(keeper.pos(), 0.8)){
			keeper.targetPos.set(keeper.pos());
			keeper.FSM().changeState(KeeperTendGoal.state());
			return;
		}
	}

	@Override
	public void exit(BaseEntity owner) {
		GoalKeeper keeper = (GoalKeeper) owner;
		keeper.AP().arriveOff();
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}
	
}
